package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HCarMapper;
import com.flea.hclv.model.dao.HResourceRelationMapper;
import com.flea.hclv.model.entity.HCar;
import com.flea.hclv.model.entity.HResourceRelation;
import com.flea.hclv.service.HCarService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.bean.FleaPage;
import com.flea.system.entity.OrgDept;
import com.flea.system.utils.FleaStringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: project
 * @description: 车辆
 * @author: shiliqiang
 * @create: 2018-09-26 20:47
 * @version: 1.0
 */
@Service
public class HCarServiceImpl implements HCarService {

    @Autowired
    private HCarMapper hCarMapper;
    @Autowired
    private HResourceRelationMapper hResourceRelationMapper;


    @Override
    public ServerResponse<String> saveOrUpdate(String userId, OrgDept currentUserDept, HCar hCar, List<HResourceRelation> hResourceRelations) {
        if (hCar==null){
            return ServerResponse.createByErrorMessage("车辆信息为空");
        }
        if (hResourceRelations.size()==0){
            return ServerResponse.createByErrorMessage("请上传行驶证照片");
        }
        String id = hCar.getId();
        int result;
        if (id == null) {
            id = FleaStringUtils.getGenerateUUID();
            hCar.setId(id);
            hCar.setDeptId(currentUserDept==null? HConstant.HCLV_ADMIN_ADMIN:currentUserDept.getDeptUuid());
            hCar.setCreateUser(userId);
            hCar.setModifyUser(userId);
            System.out.println(hCar.getRegisterTime());
            result = hCarMapper.insertSelective(hCar);
            if (result<0){
                return ServerResponse.createByErrorMessage("车辆添加失败");
            }
            return insertResourceRelation(hResourceRelations,id,userId);
        }else {
            hCar.setModifyUser(userId);
            result = hCarMapper.updateByPrimaryKeySelective(hCar);
            if (result<0){
                return ServerResponse.createByErrorMessage("车辆信息更新失败");
            }
            result =hResourceRelationMapper.deleteByUuid(id);
            if (result<0){
                return ServerResponse.createByErrorMessage("车辆行驶证更新失败");
            }
            return insertResourceRelation(hResourceRelations,id,userId);
        }
    }

    private ServerResponse<String> insertResourceRelation(List<HResourceRelation> hResourceRelations, String uuid, String userId){
        for (HResourceRelation hResourceRelation : hResourceRelations){
            String hId = FleaStringUtils.getGenerateUUID();
            hResourceRelation.setId(hId);
            hResourceRelation.setUuid(uuid);
            hResourceRelation.setCreateUser(userId);
            hResourceRelation.setModifyUser(userId);
            int result =hResourceRelationMapper.insertSelective(hResourceRelation);
            if (result<0){
                return ServerResponse.createByErrorMessage("操作失败");
            }
        }
        return ServerResponse.createBySuccessMessage("操作成功");
    }

    @Override
    public ServerResponse<FleaPage<HCar>> selectAll(OrgDept currentUserDept, int curPage, int pageSize, String name, String type) {
        RowBounds rowBounds = new RowBounds(curPage,pageSize);
        List<HCar> list = hCarMapper.selectAll(rowBounds,name,type);
        return ServerResponse.createBySuccess(new FleaPage<>(list));
    }

    @Override
    public ServerResponse<FleaPage<HCar>> selectByDeptId(OrgDept currentUserDept, int curPage, int pageSize, String name, String type) {
        RowBounds rowBounds = new RowBounds(curPage,pageSize);
        List<HCar> list;
        if (currentUserDept==null){
            return ServerResponse.createByErrorMessage("没有找到该用户的部门信息");
        }else {
            list = hCarMapper.selectByDeptId(rowBounds,currentUserDept.getDeptUuid(),name,type);
        }
        return ServerResponse.createBySuccess(new FleaPage<>(list));
    }

    @Override
    public ServerResponse<List<HResourceRelation>> selectImageById(String carId) {
        List<HResourceRelation> resourceRelations = hResourceRelationMapper.selectByUuid(carId);
        if (resourceRelations.size()==0){
            return ServerResponse.createByErrorMessage("没有找到该车辆的行车证信息");
        }
        return ServerResponse.createBySuccess(resourceRelations);
    }

    @Override
    public ServerResponse<String> deleteByIds(String userId, List<String> idList) {
        if (idList.size()==0){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        int result = hCarMapper.deleteByIds(idList,userId);
        if (result<=0){
            return ServerResponse.createByErrorMessage("删除失败");
        }
        return ServerResponse.createBySuccessMessage("删除成功");
    }
}
